﻿@model ReturnRequestModel
@using Grand.Web.Models.Orders;
@{
    Layout = "_ColumnsOne";

    //title
    Html.AddTitleParts(T("PageTitle.ReturnItems").Text);

    Html.AddCssFileParts("~/content/jquery-ui-themes/smoothness/jquery-ui.calendar.min.css");
}
<div class="page return-request-page my-3">
    <h2 class="text-xs-center text-xs-center generalTitle">@string.Format(T("ReturnRequests.Title").Text, Model.OrderNumber)</h2>
    @if (!String.IsNullOrEmpty(Model.Result))
    {
        <div class="result">
            <div class="result card bg-info p-3 text-white d-flex justify-content-center align-items-center">@Html.Raw(Model.Result)</div>
        </div>
    }
    else
    {
        @if (!String.IsNullOrEmpty(Model.Error))
        {
            <div class="result">
                <div class="result card bg-danger p-3 text-white">@Model.Error</div>
            </div>
        }

        <form asp-route="ReturnRequest" method="post">
            <div class="mt-2">
                <h2 class="h5 mt-3"><strong>@T("ReturnRequests.SelectProduct(s)")</strong></h2>
                <div class="table-responsive">
                    <table class="table table-hover data-table">
                        <colgroup>
                            <col />
                            <col width="200" />
                            <col width="150" />
                        </colgroup>
                        <thead class="table-inverse">
                            <tr>
                                <th class="product">
                                    @T("ReturnRequests.Products.Name")
                                </th>
                                <th class="unit-price">
                                    @T("ReturnRequests.Products.Price")
                                </th>
                                <th class="quantity">
                                    @T("ReturnRequests.Products.Quantity")
                                </th>
                                <th>
                                    @T("ReturnRequests.ReturnReason")
                                </th>
                                <th>
                                    @T("ReturnRequests.ReturnAction")
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var item in Model.Items)
                            {
                                <tr>
                                    <td class="product">
                                        <a href="@Url.RouteUrl("Product", new { SeName = item.ProductSeName })">@item.ProductName</a>
                                        @if (!String.IsNullOrEmpty(item.AttributeInfo))
                                        {
                                            <div class="attributes">
                                                @Html.Raw(item.AttributeInfo)
                                            </div>
                                        }
                                        @if (!String.IsNullOrEmpty(item.VendorName))
                                        {
                                            <div class="vendor">
                                                @item.VendorName
                                            </div>
                                        }
                                    </td>
                                    <td class="unit-price">
                                        @item.UnitPrice
                                    </td>
                                    <td class="quantity">
                                        <select class="custom-select form-control" name="quantity@(item.Id)">
                                            @for (int i = 0; i <= item.Quantity; i++)
                                            {
                                                <option value="@(i)">@(i)</option>
                                            }
                                        </select>
                                    </td>
                                    <td>
                                        @{
                                            var reasonsSelectList = Model.AvailableReturnReasons.Select(x => new SelectListItem
                                            {
                                                Text = x.Name,
                                                Value = x.Id.ToString()
                                            }).ToList();
                                        }
                                        <select asp-for="@item.ReturnRequestReasonId" asp-items="@reasonsSelectList" name="reason@(item.Id)" class="form-control custom-select return-reasons"></select>
                                    </td>
                                    <td>
                                        @{
                                            var actionsSelectList = Model.AvailableReturnActions.Select(x => new SelectListItem
                                            {
                                                Text = x.Name,
                                                Value = x.Id.ToString()
                                            }).ToList();
                                        }
                                        <select asp-for="@item.ReturnRequestReasonId" asp-items="@actionsSelectList" name="action@(item.Id)" class="form-control custom-select return-actions"></select>
                                    </td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="form-fields">
                <div class="form-group">
                    <label asp-for="Comments" class="col-form-label">@T("ReturnRequests.Comments"):</label>
                    <textarea asp-for="Comments" class="form-control comment"></textarea>
                    <span asp-validation-for="Comments"></span>
                </div>
                @if (Model.ShowPickupAddress)
                {
                    <div class="form-group">
                        <div class="pickup-addresses" id="pickup-addresses-form">
                            @if (Model.ExistingAddresses.Any())
                            {
                                <div class="section select-pickup-address">
                                    <label for="pickup-address-select">@T("ReturnRequests.SelectPickupAddressOrEnterNewOne")</label>
                                    <div>
                                        <select name="pickup_address_id" id="pickup-address-select" class="custom-select address-select"
                                                title="" onchange="newAddress(!this.value)">
                                            @foreach (var address in Model.ExistingAddresses)
                                            {
                                                var addressLine = "";
                                                addressLine += address.FirstName;
                                                addressLine += " " + address.LastName;
                                                if (address.StreetAddressEnabled && !String.IsNullOrEmpty(address.Address1))
                                                {
                                                    addressLine += ", " + address.Address1;
                                                }
                                                if (address.CityEnabled && !String.IsNullOrEmpty(address.City))
                                                {
                                                    addressLine += ", " + address.City;
                                                }
                                                if (address.StateProvinceEnabled && !String.IsNullOrEmpty(address.StateProvinceName))
                                                {
                                                    addressLine += ", " + address.StateProvinceName;
                                                }
                                                if (address.ZipPostalCodeEnabled && !String.IsNullOrEmpty(address.ZipPostalCode))
                                                {
                                                    addressLine += " " + address.ZipPostalCode;
                                                }
                                                if (address.CountryEnabled && !String.IsNullOrWhiteSpace(address.CountryName))
                                                {
                                                    addressLine += ", " + address.CountryName;
                                                }
                                                <option selected="@(!Model.NewAddressPreselected && Model.ExistingAddresses[0].Id == address.Id)" value="@(address.Id)">@(addressLine)</option>
                                            }
                                            <option value="" selected="@Model.NewAddressPreselected">@T("ReturnRequests.NewAddress")</option>
                                        </select>
                                    </div>
                                </div>
                            }
                            <div class="section new-pickup-address" id="pickup-new-address-form">
                                <div class="enter-address">
                                    <div asp-validation-summary="ModelOnly" class="message-error"></div>
                                    @{
                                        var dataDictAddress = new ViewDataDictionary(ViewData);
                                        dataDictAddress.TemplateInfo.HtmlFieldPrefix = "ReturnRequestNewAddress";
                                        <partial name="_CreateOrUpdateAddress" model="Model.NewAddress" view-data="dataDictAddress" />
                                    }
                                </div>
                            </div>
                        </div>
                    </div>
                }
                @if (Model.ShowPickupDate)
                {
                    <div class="form-group">
                        @T("ReturnRequests.PickupDate"):
                        <input asp-for="PickupDate" type="date" class="form-control datepicker" required="@Model.PickupDateRequired"/>
                    </div>
                }
            </div>
            <div class="row">
                <div class="col-12 btn-group mt-1 mb-1">
                    <input type="submit" name="submit-return-request" class="btn btn-secondary submit-return-request-button" value="@T("ReturnRequests.Submit")" />
                </div>
            </div>
        </form>
    }
</div>
<script asp-location="Footer">
    $(document).ready(function () {
        if ($("#pickup-address-select option:selected").val() != "") {
            newAddress(false);
        }
    });
</script>